package net.luohuasheng.bee.proxy.core.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BeanUtils {
    public static void copy(Object desc, Object source) {
        if (desc != null && source != null) {
            for (Field declaredField : getAllFields(desc)) {
                declaredField.setAccessible(true);
                for (Field field : getAllFields(source)) {
                    field.setAccessible(true);
                    if (field.getName().equals(declaredField.getName()) && field.isAccessible() && declaredField.isAccessible()) {
                        try {
                            declaredField.set(desc, field.get(source));
                        } catch (Exception ignored) {
                         }
                    }
                }
            }
        }
    }

    private static List<Field> getAllFields(Object object) {
        List<Field> fields = new ArrayList<>();
        Class<?> classZ = object.getClass();
        while (!classZ.equals(Object.class)) {
            fields.addAll(Arrays.asList(classZ.getDeclaredFields()));
            classZ = classZ.getSuperclass();
        }
        return fields;
    }
}
